package systempen.cmanager;

import java.util.List;
import systempen.cmanager.R;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class BudgetListAdapter extends ArrayAdapter<BudgetListItem> {

	private List<BudgetListItem> items;

	public BudgetListAdapter(Context context, int textViewResourceId, List<BudgetListItem> items) {
		super(context, textViewResourceId, items);
		this.items = items;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.budgetlist_row, null);
		}
		BudgetListItem o = items.get(position);
		if (o != null) {
			TextView tvType = (TextView) v.findViewById(R.id.textBudgetType);
			if (tvType != null){
				tvType.setText(o.get_costType());
			}
			TextView tvID = (TextView) v.findViewById(R.id.textBudgetID);
			if (tvID != null){
				tvID.setText(o.get_budgetCode());
			}
			
			TextView tvTypeName = (TextView) v.findViewById(R.id.textCostTypeN);
			if (tvTypeName != null) {
				tvTypeName.setText(util.getLevelName(o.get_costType(), getContext()));
			}
			TextView tvTypePath = (TextView) v.findViewById(R.id.textTypePathN);
			if (tvTypePath != null) {
				tvTypePath.setText(util.getPathOfLevel1(o.get_costType(), getContext()));
			}
			TextView etBudgetDescription = (TextView) v.findViewById(R.id.textDescriptionBudgetN);
			if (etBudgetDescription != null) {
				etBudgetDescription.setText(o.get_budgetDescription());
			}
			TextView tvAccountSum = (TextView) v.findViewById(R.id.textBudgetAmount);
			if (tvAccountSum != null) { 
				tvAccountSum.setText(o.get_budgetAmount());
			}
			TextView tvDateFrom = (TextView) v.findViewById(R.id.textBudgetDateFrom);
			if (tvDateFrom != null) {
				tvDateFrom.setText(o.get_dateFrom());
			}
			TextView tvDateTo = (TextView) v.findViewById(R.id.textBudgetDateTo);
			if (tvDateTo != null) {
				tvDateTo.setText(o.get_dateTo());
			}
			TextView tvAccount = (TextView) v.findViewById(R.id.textAccountBudgetN);
			if (tvAccount != null){
				tvAccount.setText(o.get_account());
			}
			
			
			ProgressBar progressBudget = (ProgressBar) v.findViewById(R.id.progressBudget);
			String accountcode = util.getBudgetAccountCode(o.get_budgetCode(), getContext());
			float costSum = util.getCostSumF(o.get_costType(), util.viewStringDateToDBStringDate(o.get_dateFrom(),getContext()), util.viewStringDateToDBStringDate(o.get_dateTo(),getContext()), getContext(), accountcode);
			if (progressBudget != null){
				
				String budget = o.get_budgetAmount();
				budget = Float.toString(util.getNumber("", budget));
				/*
				if (budget.contains(",")&&((budget.contains(".")))){
					budget = budget.replaceFirst(",","");
				}
				if (budget.contains(",")&&((budget.contains(".")))){
					budget = budget.replaceFirst(",","");
				}
				if (budget.contains(",")&&((budget.contains(".")))){
					budget = budget.replaceFirst(",","");
				}
				*/

				float fbudget = Float.parseFloat(budget);
				float percentage = costSum / fbudget * 100;
				progressBudget.setProgress((int) percentage);
			}
			TextView tvSpent = (TextView) v.findViewById(R.id.textBudgetSpent);
			if (tvSpent != null){
				tvSpent.setText(Float.toString(costSum));
			}
		}
		return v;
	}
}